﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProgressReportUtils.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the ProgressReportUtils type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Utilities
{
    using System;

    using Collaboris.Saf.Engine;
    using Collaboris.Saf.Tracing;
    using Collaboris.Utils;

    using Engine.Report;

    public class ProgressReportUtils
    {
        private static TraceHelper trace;
        
        /// <summary>
        /// Provides and instance of a Trace Helper
        /// </summary>
        protected static TraceHelper Trace
        {
            get
            {
                if (trace == null)
                    trace = new SafTraceHelper("Collaboris.Saf.Utilities.ProgressReportUtils");

                return trace;
            }
        }

        /// <summary>
        /// Updates a <see cref="StatusReport"/>.
        /// </summary>
        /// <returns></returns>
        public static T FinishReport<T>(IProgressReport report, ProcessStatus status)
        {
            Trace.TraceMethodStart("ProgressReportUtils.FinishReport()");

            report.Status = status;
            report.EndDateTime = DateTime.Now;

            Trace.TraceMethodEnd("ProgressReportUtils.FinishReport()");
            return (T)report;
        }

        /// <summary>
        /// Creates a new <see cref="StatusReport"/>.
        /// </summary>
        /// <returns></returns>
        public static T StartReport<T>(ProcessMode mode, string actionName, Guid id) where T : IProgressReport, new()
        {
            Trace.TraceMethodStart("ProgressReportUtils.CreateReport()");
            
            IProgressReport report = new T();
            report.Id = id;
            report.Mode = mode;
            report.Name = actionName;
            report.StartDateTime = DateTime.Now;
            
            Trace.TraceMethodEnd("ProgressReportUtils.CreateReport()");
            return (T)report ;
        }
    }
}